package com.evon.yardmanagement.ui.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.evon.yardmanagement.service.OEMService;
import com.evon.yardmanagement.service.UtilityService;
import com.evon.yardmanagement.service.VendorManagementService;
import com.evon.yardmanagement.ui.dto.CountryDTO;
import com.evon.yardmanagement.ui.dto.OemDTO;
import com.evon.yardmanagement.ui.dto.StateDTO;
import com.evon.yardmanagement.ui.dto.VendorDTO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

/**
 * Action class responsible for managing OEMs.
 * 
 * @author vgaurav
 * 
 */

public class OEMManagementAction extends SuperAdminFilterAction implements
		Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private OemDTO dto;

	private List<OemDTO> oemList = new ArrayList<OemDTO>();

	private List<StateDTO> listState = new ArrayList<StateDTO>();
	private List<CountryDTO> listCountry = new UtilityService().getCountries();
	private List<VendorDTO> listVendor = new ArrayList<VendorDTO>();
	private int selectedStateId;

	private int selectedCountryId;

	/**
	 * Initialize the lists, for maintaining it if validation fails.
	 */
	@Override
	public void prepare() throws Exception {
		// populate state list
		listCountry = new UtilityService().getCountries();
		// set selected state
		listState = UtilityService.getState(selectedCountryId);
		// populate vendor
		if (SecurityUtils.getSubject().hasRole("SUPER_ADMIN")) {
			listVendor = VendorManagementService.getAllVendors();
		} else {
			listVendor.add(VendorManagementService.getCurrentVendorDTO());
		}
		
	//	populateValues();

	}

	@SkipValidation
	public String init() {
		super.init();
		if (!SecurityUtils.getSubject().hasRole("SUPER_ADMIN"))
			oemList = OEMService.getAllOem();
		return SUCCESS;
	}

	@SkipValidation
	public String back() {
		super.init();
		if (!SecurityUtils.getSubject().hasRole("SUPER_ADMIN"))
			oemList = OEMService.getAllOem();
		return SUCCESS;
	}

	@SkipValidation
	public String emptyOEM() {
		dto = new OemDTO();
		// set the vendor
		dto.setVendor(VendorManagementService.getCurrentVendorDTO());
		// dto.setVendor()
		// populate State list
		listCountry = new UtilityService().getCountries();

		// populate vendor
		if (SecurityUtils.getSubject().hasRole("SUPER_ADMIN")) {
			listVendor = VendorManagementService.getAllVendors();
		} else {
			listVendor.add(VendorManagementService.getCurrentVendorDTO());
		}

		return SUCCESS;
	}

	/**
	 * Show the details of the selected OEM.
	 */
	@SkipValidation
	public String execute() {
		dto = OEMService.getOemDetails(dto.getOemId());
		// populate state list
		listCountry = new UtilityService().getCountries();

		// get OEM country
		selectedCountryId = dto.getState().getCountry().getId();

		// set selected state
		listState = UtilityService.getState(selectedCountryId);
		selectedStateId = dto.getState().getId();

		// populate vendor
		if (SecurityUtils.getSubject().hasRole("SUPER_ADMIN")) {
			listVendor = VendorManagementService.getAllVendors();
		} else {
			listVendor.add(VendorManagementService.getCurrentVendorDTO());
		}

		return SUCCESS;
	}

	@SkipValidation
	public String showState() {
		listCountry = new UtilityService().getCountries();

		// selectedCountryId = dto.getState().getCountry().getId();

		// set selected state
		listState = UtilityService.getState(selectedCountryId);

		// populate vendor
		if (SecurityUtils.getSubject().hasRole("SUPER_ADMIN")) {
			listVendor = VendorManagementService.getAllVendors();
		} else {
			listVendor.add(VendorManagementService.getCurrentVendorDTO());
		}

		return SUCCESS;
	}

	@Validations(requiredStrings = {
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.oemName", message = "You must enter a character name."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.city", message = "You must enter a city name."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.phone", message = "You must enter a phone number."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.address", message = "You must enter address."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.zipCode", message = "You must enter zip code."),
			@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "dto.emailId", message = "You must enter email id.") }, regexFields = {
			@RegexFieldValidator(fieldName = "dto.zipCode", expression = "^[0-9]+", message = "Phone can only be numeric", type = ValidatorType.FIELD),
			@RegexFieldValidator(fieldName = "dto.zipCode", expression = "^[0-9]+", message = "Zip code can only be numeric", type = ValidatorType.FIELD) })
	@StringLengthFieldValidator(fieldName = "dto.phone", message = "Phone number must be between 9 and 12 characters", minLength = "9", maxLength = "12")
	@EmailValidator(fieldName = "dto.emailId", type = ValidatorType.FIELD, message = "You must enter proper email id")
	public String addOEM() {
		// TODO: move this to global data
		listCountry = new UtilityService().getCountries();
		listState = UtilityService.getState(selectedCountryId);

		if (!validInput()) {
			return "input";
		}

		boolean isUpdate = dto.getOemId() > 0;
		OEMService.addOem(dto);
		if (isUpdate) {
			addActionMessage("Successfully Updated the value");
		} else {
			addActionMessage("Added Successfully");
		}
		return "redirect";
	}

	private boolean validInput() {
		if (selectedCountryId <= 0) {
			addActionMessage("Please select a country");
			return false;
		}
		if (dto.getState().getId() <= 0) {
			addActionMessage("Please select a state");
			return false;
		}
		return true;
	}

	public OemDTO getDto() {
		return dto;
	}

	@SkipValidation
	public void setDto(OemDTO dto) {
		this.dto = dto;
	}

	public List<OemDTO> getOemList() {
		return oemList;
	}

	@SkipValidation
	public void setOemList(List<OemDTO> oemList) {
		this.oemList = oemList;
	}

	public List<StateDTO> getListState() {
		return listState;
	}

	public List<CountryDTO> getListCountry() {
		return listCountry;
	}

	public int getSelectedStateId() {
		return selectedStateId;
	}

	public int getSelectedCountryId() {
		return selectedCountryId;
	}

	public void setListState(List<StateDTO> listState) {
		this.listState = listState;
	}

	@SkipValidation
	public void setListCountry(List<CountryDTO> listCountry) {
		this.listCountry = listCountry;
	}

	@SkipValidation
	public void setSelectedStateId(int selectedStateId) {
		this.selectedStateId = selectedStateId;
	}

	@SkipValidation
	public void setSelectedCountryId(int selectedCountryId) {
		this.selectedCountryId = selectedCountryId;
	}

	public List<VendorDTO> getListVendor() {
		return listVendor;
	}

	@SkipValidation
	public void setListVendor(List<VendorDTO> listVendor) {
		this.listVendor = listVendor;
	}

	@SkipValidation
	@Override
	public String onVendorSelect() {
		oemList = OEMService.getOems( getSelectedAdminVendorId());
		populateValues();
		return SUCCESS;
	}

}
